/*
 * Golazo MA4
 * Copyright (C) 2008 Golazo MASHUP AWARD 4th team.
 * http://code.google.com/p/golazo/
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License 
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * checkNote
 */
function checkNote() {
  
  var note = $('#form_note');
  
  var message = $('#form_message')
    .hide(); 

  if (!Golazo.Action.checkNote(note.val())) {
    
	message
	  .text('メモに誤りがあります。メモを確認してください。')
	  .show();
	
	return false;
  }

  return true;
};
checkNote.runat = 'both';

/**
 * serverload
 */
function serverload() {

  // dispatch 
  Golazo.dispatch(function(request, response, session) {

    // 私は誰か
    var me = Golazo.User.find(session.get('me'));
	if (!me)
	  return $('#signin')
	    .layout(/*me*/)
	    .metaRefresh('/signin.html?' + $.param({ url: request.uri }), 3);

    // メモの表示位置
	var startIndex = request.data.startIndex;
	if (!Golazo.checkDigit(startIndex))
	  startIndex = 1;
	else
	  startIndex = parseInt(startIndex, 10);
	if (startIndex < 1)
	  startIndex = 1;

    // メモの表示件数
    var maxResults = request.data.maxResults;
	if (!Golazo.checkDigit(maxResults))
	  maxResults = 10;
	else
	  maxResults = parseInt(maxResults, 10);
	if (maxResults < 1 || maxResults > 100)
	  maxResults = 10;

    // メモの総件数
    var totalResults = Golazo.Action.countByMe(me, {});

    // メモを検索する
    var actions = Golazo.Action.findAllByMe(me, startIndex - 1, maxResults, {});
	
	// メモをリスト表示する
	actions.forEach(function(action) {

      // メモの購読者を検索する
      var subscribers = action.findBySubscribed();

      // メモの購読者と作成者
      action.subscribers = subscribers;
	  action.creator = Golazo.find(subscribers, function() {
        return this.equals(action.creatorId);
      });

      // 編集、削除できるか
      if (me.equals(action.creator)) 
	    action.editable = true;

      Golazo.Action.htmlify(action)
		.appendTo('#actions')
	    .show();
    });

	if (totalResults > 0)
	  $('#pager')
	    .pager('/me.html', startIndex, maxResults, totalResults)
		.show();
	else
	  $('#summary')
	    .toggle();

    return $('#succeeded,#me')
	  .layout(me);
  });
};

/**
 * load
 */
function load(layout) {

  $(document.form).submit(function() {

    // メモは期待どおりか
	if (!checkNote())
	  return false;
	
    var note = $('#form_note');
      
    // メモを保存する
	var action;
	try { 
	  action = addAction(note.val());
	} catch (e) {
	  alert(''+e);
	  return false;
	}
	  
	note.val('');

	Golazo.Action.bind(Golazo.Action.htmlify(action))
	  .prependTo('#actions')
	  .fadeIn('slow');

    return false;
  });

  $('div.action', '#actions').each(function() {
    Golazo.Action.bind(this);
  });
};
load.runat = 'client';
